package com.surfilter.massdata.spark.util.ipcheck;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.spark.api.java.JavaPairRDD;
import org.apache.spark.api.java.JavaRDD;
import org.apache.spark.api.java.function.FlatMapFunction;
import org.apache.spark.api.java.function.Function;
import org.apache.spark.api.java.function.PairFunction;
import org.apache.spark.api.java.function.VoidFunction;
import org.apache.spark.sql.DataFrame;
import org.apache.spark.sql.SQLContext;
import org.apache.spark.storage.StorageLevel;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import com.surfilter.massdata.spark.bean.ipcheck.ErrIp;
import com.surfilter.massdata.spark.bean.ipcheck.IpBean;
import com.surfilter.massdata.spark.bean.ipcheck.ObjectInfo;

import scala.Tuple2;
import scala.Tuple3;

/** 
* @author  hutao
* @date 创建时间：2017年5月31日 上午10:00:07 
* @Description：
* @version 1.0 
* 
**/
public class IpCheckSync {
	public static final String BEIAN_BEIAN_SOURCE = IpCheckConstants.BEIAN + "_" + IpCheckConstants.IP_SOURCE_TYPE;
	public static final String BEIAN_BEIAN_ALLOCATE = IpCheckConstants.BEIAN + "_" + IpCheckConstants.IP_ALLOCATE_TYPE;
	public static final String UPLOAD_UPLOAD_SOURCE = IpCheckConstants.UPLOAD + "_" + IpCheckConstants.IP_SOURCE_TYPE;
	public static final String UPLOAD_UPLOAD_ALLOCATE = IpCheckConstants.UPLOAD + "_" + IpCheckConstants.IP_ALLOCATE_TYPE; 
	public static  JavaRDD<IpBean> dealBusiness(JavaRDD<IpBean> ipBeanRdd,SQLContext sc) throws Exception {
		//JavaRDD<IpBean>转换为JavaPairRDD<Long, Iterable<IpBean>>为了对ip进行分组
		JavaPairRDD<Long, Iterable<IpBean>> pariRDD = IpCheckUtil.transJavaPariRdd(ipBeanRdd);
		 JavaRDD<Tuple3<Object, Boolean, Long>> newRdd=pariRDD.flatMap(new FlatMapFunction<Tuple2<Long,Iterable<IpBean>>, Tuple3<Object,Boolean,Long>>() {

			private static final long serialVersionUID = 1L;

			@Override
			public Iterable<Tuple3<Object, Boolean, Long>> call(Tuple2<Long, Iterable<IpBean>> tuple) throws Exception {
				// TODO Auto-generated method stub
				long ip = tuple._1;
				Iterable<IpBean> its = tuple._2;
				Map<String, List<IpBean>> map = Maps.newHashMap();
				for (IpBean ipBean : its) {
//					IpBean ipBean = new IpBean();
//					ipBean = ConvertUtil.copy(ipBean, localIpBean);
					if(ipBean.getStorageType().equals(IpCheckConstants.BEIAN)){
						if(ipBean.getTflag().equals(IpCheckConstants.IP_SOURCE_TYPE)){
							List<IpBean> list1 = map.get(BEIAN_BEIAN_SOURCE);
							if(list1 == null){
								list1 = Lists.newArrayList();
							}
							list1.add(ipBean);
							map.put(BEIAN_BEIAN_SOURCE, list1);
						}else if(ipBean.getTflag().equals(IpCheckConstants.IP_ALLOCATE_TYPE)){
							List<IpBean> list2 = map.get(BEIAN_BEIAN_ALLOCATE);
							if(list2 == null){
								list2 = Lists.newArrayList();
							}
							list2.add(ipBean);
							map.put(BEIAN_BEIAN_ALLOCATE, list2);
						}
					}else if(ipBean.getStorageType().equals(IpCheckConstants.UPLOAD)){
						if(ipBean.getTflag().equals(IpCheckConstants.IP_SOURCE_TYPE)){
							List<IpBean> list3 = map.get(UPLOAD_UPLOAD_SOURCE);
							if(list3 == null){
								list3 = Lists.newArrayList();
							}
							list3.add(ipBean);
							map.put(UPLOAD_UPLOAD_SOURCE, list3);
						}else if(ipBean.getTflag().equals(IpCheckConstants.IP_ALLOCATE_TYPE)){
							List<IpBean> list4 = map.get(UPLOAD_UPLOAD_ALLOCATE);
							if(list4 == null){
								list4 = Lists.newArrayList();
							}
							list4.add(ipBean);
							map.put(UPLOAD_UPLOAD_ALLOCATE, list4);
						}
					}
				}
				Set<Tuple3<Object,Boolean,Long>> resultSet=new HashSet<Tuple3<Object,Boolean,Long>>();
				Set<IpBean> errIpBeanSet=Sets.newHashSet();
				Set<IpBean> rightTempIpBeanSet=Sets.newHashSet();
				//备案来源库vs上报来源库
				if(null!=map.get(BEIAN_BEIAN_SOURCE)){
					ObjectInfo objectInfo=dealMerge(map, BEIAN_BEIAN_SOURCE, UPLOAD_UPLOAD_SOURCE, ip, "SOURCE");
					deal(objectInfo,resultSet,errIpBeanSet,rightTempIpBeanSet,ip);
				}
//				
				//备案分配库vs上报分配库
				if(null!=map.get(BEIAN_BEIAN_ALLOCATE)){
					ObjectInfo objectInfo=dealMerge(map, BEIAN_BEIAN_ALLOCATE, UPLOAD_UPLOAD_ALLOCATE, ip, "ALLOCATE");
					deal(objectInfo,resultSet,errIpBeanSet,rightTempIpBeanSet,ip);
				}
				
				//上报来源库vs备案来源库
				if(null!=map.get(UPLOAD_UPLOAD_SOURCE)){
					ObjectInfo objectInfo=dealMerge(map, UPLOAD_UPLOAD_SOURCE, BEIAN_BEIAN_SOURCE, ip, "SOURCE");
					deal(objectInfo,resultSet,errIpBeanSet,rightTempIpBeanSet,ip);
				}
				
				
//				//上报分配库vs备案分配库
				if(null!=map.get(UPLOAD_UPLOAD_ALLOCATE)){
					ObjectInfo objectInfo=dealMerge(map, UPLOAD_UPLOAD_ALLOCATE, BEIAN_BEIAN_ALLOCATE, ip,"ALLOCATE");
					deal(objectInfo,resultSet,errIpBeanSet,rightTempIpBeanSet,ip);
				}
				
				for(IpBean bean:rightTempIpBeanSet){
					if(!errIpBeanSet.contains(bean)){
						resultSet.add(new Tuple3<Object,Boolean,Long>(bean,true,ip));
					}
				}
				
				
				return resultSet;
			}

		});
		 
		 newRdd.persist(StorageLevel.MEMORY_AND_DISK_SER());
		 JavaPairRDD<String, Long> errRdd=newRdd.filter(new Function<Tuple3<Object,Boolean,Long>, Boolean>() {
				private static final long serialVersionUID = 1L;

				public Boolean call(Tuple3<Object, Boolean, Long> v1) throws Exception {
					Boolean flag=v1._2();
					boolean result=false;
					if(null!=flag&&flag==false){
						result=true;
					}
					return result;
				}
			}).mapToPair(new PairFunction<Tuple3<Object,Boolean,Long>, String, Long>() {

				private static final long serialVersionUID = 1L;

				@Override
				public Tuple2<String, Long> call(Tuple3<Object, Boolean, Long> tuple3) throws Exception {
					// TODO Auto-generated method stub
					ErrIp errIp=(ErrIp)tuple3._1();
					return new Tuple2<String,Long>(errIp.toString(),tuple3._3());
				}
			
			});
			 JavaRDD<ErrIp> conflictRdd= errRdd.groupByKey().flatMap(new FlatMapFunction<Tuple2<String,Iterable<Long>>, ErrIp>() {
				private static final long serialVersionUID = 1L;

				@Override
				public Iterable<ErrIp> call(Tuple2<String, Iterable<Long>> tuple2) throws Exception {
					String conflictStr=tuple2._1;
					Iterable<Long> its=tuple2._2;
					List<ErrIp> resultList=new ArrayList<ErrIp>();
					String[] array=conflictStr.split("\t");
					ErrIp errIp = new ErrIp();
					if(array.length==5){
						errIp = ConvertUtil.convertErrIp(array, errIp);
					}
					
					List<Long> list=new ArrayList<Long>();
					for(Long ip:its){
						list.add(ip);
					}
					List<String> segList=IpUtils.getIpSeg(list);
					for(String seg:segList){
						long qsip=Long.parseLong(seg.split(",")[0]);
						long zzip=Long.parseLong(seg.split(",")[1]);
						ErrIp source = ConvertUtil.copyErrIp(new ErrIp(), errIp);
						source.setQsip(qsip);
						source.setZzip(zzip);
						resultList.add(source);
					}
					return resultList;
				}
			});
			 DataFrame conlinctDF=sc.createDataFrame(conflictRdd, ErrIp.class);
			 IpCheckUtil.writeConflictToTable(conlinctDF);//将冲突数据入库
//打印冲突数据			 
//			 conflictRdd.foreach(new VoidFunction<ErrIp>() {
//					
//					@Override
//					public void call(ErrIp t) throws Exception {
//						// TODO Auto-generated method stub
//						System.err.println("error>>>>>>"+t.getQsip()+"\t"+t.getZzip()+"\t"+t.getBbdws()+"\t"+t.getConflictType()+"\t"+t.getRegionIds()+"\t"+t.getStorageType()+"\t"+t.getJsonStr());
//					}
//				});
		 
		 
// #########################################################################################################
 JavaPairRDD<String, Long> rightRdd=newRdd.filter(new Function<Tuple3<Object,Boolean,Long>, Boolean>() {
	private static final long serialVersionUID = 1L;

	public Boolean call(Tuple3<Object, Boolean, Long> v1) throws Exception {
		Boolean flag=v1._2();
		return null!=flag&&flag==true?true:false;
	}
}).mapToPair(new PairFunction<Tuple3<Object,Boolean,Long>, String, Long>() {

	private static final long serialVersionUID = 1L;

	@Override
	public Tuple2<String, Long> call(Tuple3<Object, Boolean, Long> tuple3) throws Exception {
		// TODO Auto-generated method stub
		IpBean bean=(IpBean)tuple3._1();
		bean.setIsPass(null);
		return new Tuple2<String,Long>(bean.toNewString(),tuple3._3());
	}

});
 JavaRDD<IpBean> resultRdd= rightRdd.groupByKey().flatMap(new FlatMapFunction<Tuple2<String,Iterable<Long>>, IpBean>() {
	private static final long serialVersionUID = 1L;

	@Override
	public Iterable<IpBean> call(Tuple2<String, Iterable<Long>> tuple2) throws Exception {
		String beanStr=tuple2._1;
		Iterable<Long> its=tuple2._2;
		List<IpBean> resultList=new ArrayList<IpBean>();
		String[] array=beanStr.split("\t");
		IpBean ipBean = new IpBean();
		if(array.length==46){
			ipBean = ConvertUtil.convertIpBean(array, ipBean);
			List<Long> list=new ArrayList<Long>();
			for(Long ip:its){
				list.add(ip);
			}
			List<String> segList=IpUtils.getIpSeg(list);
			for(String seg:segList){
				long qsip=Long.parseLong(seg.split(",")[0]);
				long zzip=Long.parseLong(seg.split(",")[1]);
				IpBean source = ConvertUtil.copy(new IpBean(), ipBean);
				source.setQsip(qsip);
				source.setZzip(zzip);
				resultList.add(source);
			}
		}
	
		return resultList;
	}
});
//打印正确数据 
//resultRdd.foreach(new VoidFunction<IpBean>() {
//	
//	@Override
//	public void call(IpBean t) throws Exception {
//		// TODO Auto-generated method stub
//		System.err.println("right>>>>>>"+t.toString());
//	}
//});
		return resultRdd;
	}
	public static ObjectInfo dealMerge(Map<String, List<IpBean>> map, String type1, String type2, Long ip, String checkType) throws IOException, InterruptedException{
		ObjectInfo obj=new ObjectInfo();
		Set<ErrIp> errIpSet=Sets.newHashSet();
		Set<IpBean> errIpBeanSet=new HashSet<IpBean>();
		Set<IpBean> rightIpBeanSet=new HashSet<IpBean>();
		obj.setErrIpBeanSet(errIpBeanSet);;
		obj.setErrIpSet(errIpSet);
		obj.setRightIpBeanSet(rightIpBeanSet);
		if(map.get(type1) == null){
			return obj;
		}else if(map.get(type1) != null){
			if(map.get(type2) == null){
				List<IpBean> list1 = map.get(type1);
				for(IpBean bean1 : list1){
//					if(null==bean1.getIsPass()|| bean1.getIsPass()== true){
//						bean1.setIsPass(false);
//						
						if(bean1.getTflag().equals(IpCheckConstants.IP_SOURCE_TYPE)){
//							if(redisDao.hget(IpCheckConstants.CONFLICT_DATA_KEY, ip.toString()) == null){
								ErrIp errIp = new ErrIp();
//								String mapKey = "";
								if(bean1.getBbdw_id() > bean1.getLyid()){
//									mapKey = bean1.getBbdw_id() + "_" + bean1.getLyid() + "_" + bean1.getStorageType() + "_" + bean1.getTflag() + "_" +  bean1.getId();
									errIp.setBbdws(bean1.getBbdw_id().toString());
								}else{
//									mapKey = bean1.getLyid() + "_" + bean1.getBbdw_id() + "_" + bean1.getStorageType() + "_" + bean1.getTflag() + "_" +  bean1.getId();
									errIp.setBbdws(bean1.getBbdw_id().toString());
								}
								errIp.setConflictType(ConflictType.IP_BEIAN_UPLOAD_CONFLICT_ERROR.getTypeValue());
								errIp.setRegionIds(bean1.getStorageType() + "_" + bean1.getTflag() + "_" + bean1.getId());
								errIp.setJsonStr(bean1.getConflictMsg());
								errIp.setStorageType(bean1.getStorageType());
								errIpSet.add(errIp);
								errIpBeanSet.add(bean1);
//							}
						}else if(bean1.getTflag().equals(IpCheckConstants.IP_ALLOCATE_TYPE)){
//							if(redisDao.hget(IpCheckConstants.CONFLICT_DATA_KEY, ip.toString()) == null){
							ErrIp errIp = new ErrIp();
//								String mapKey = "";
//								if(bean1.getBbdw_id() > bean1.getFpdx()){
//									errIp.setBbdws(bean1.getBbdw_id().toString());
//									mapKey = bean1.getBbdw_id() + "_" + bean1.getFpdx() + "_" + bean1.getStorageType() + "_" + bean1.getTflag() + "_" +   bean1.getId();
//								}else{
									errIp.setBbdws(bean1.getBbdw_id().toString());
//									mapKey = bean1.getFpdx() + "_" + bean1.getBbdw_id() + "_" + bean1.getStorageType() + "_" + bean1.getTflag() + "_" +   bean1.getId();
//								}
								errIp.setBbdws(bean1.getBbdw_id().toString());
								errIp.setConflictType(ConflictType.IP_BEIAN_UPLOAD_CONFLICT_ERROR.getTypeValue());
								errIp.setRegionIds(bean1.getStorageType() + "_" + bean1.getTflag() + "_" + bean1.getId());
								errIp.setJsonStr(bean1.getConflictMsg());
								errIp.setStorageType(bean1.getStorageType());
								errIpSet.add(errIp);
								errIpBeanSet.add(bean1);
							}
//						}
					}//end if
				}else{

					List<IpBean> list1 = map.get(type1);
					List<IpBean> list2 = map.get(type2);
					if(checkType.contains("SOURCE")){
						for(IpBean ipBean1 : list1){
							if(null==ipBean1.getIsPass()|| ipBean1.getIsPass()== true){
								boolean flag = false;
								for(IpBean ipBean2 : list2){
									if(null==ipBean2.getIsPass()|| ipBean2.getIsPass()== true){
										if(!ipBean1.getBbdw_id().equals(ipBean2.getBbdw_id()) || !ipBean1.getLyid().equals(ipBean2.getLyid())){
											flag = false;
										}else{
											flag = true;
											if(ipBean1.getStorageType().equals(IpCheckConstants.BEIAN)){
												rightIpBeanSet.add(ipBean2);
//												obj.setRightIpBean(ipBean2);
//												this.mergeSeg(StorageType.SOURCE_CORRECT.toString(), ipBean2, ip, mapkey2, correctMap);
											}else if(ipBean1.getStorageType().equals(IpCheckConstants.UPLOAD)){
//												String mapkey1 = (ipBean1.getBbdw_id() > ipBean1.getLyid() ? ipBean1.getBbdw_id() + "_" + ipBean1.getLyid() : ipBean1.getLyid() + "_" + ipBean1.getBbdw_id()) + "_" + ipBean1.getStorageType() + "_" + ipBean1.getTflag() + "_" + ipBean1.getId();
//												this.mergeSeg(StorageType.SOURCE_CORRECT.toString(), ipBean1, ip, mapkey1, correctMap);
												rightIpBeanSet.add(ipBean1);
//												obj.setRightIpBean(ipBean1);
											}
											break;
										}// else
									}else{
										continue;
									}
								}//end for
								if(!flag){
									//冲突
									ipBean1.setIsPass(false);
//									if(redisDao.hget(IpCheckConstants.CONFLICT_DATA_KEY, ip.toString()) == null){
									ErrIp errIp = new ErrIp();
//										String mapKey = "";
										if(ipBean1.getBbdw_id() > ipBean1.getLyid()){
											errIp.setBbdws(ipBean1.getBbdw_id().toString());
//											mapKey = ipBean1.getBbdw_id() + "_" + ipBean1.getLyid() + "_" + ipBean1.getStorageType() + "_" + ipBean1.getTflag() + "_" + ipBean1.getId();
										}else{
											errIp.setBbdws(ipBean1.getBbdw_id().toString());
//											mapKey = ipBean1.getLyid() + "_" + ipBean1.getBbdw_id() + "_" + ipBean1.getStorageType() + "_" + ipBean1.getTflag() + "_" + ipBean1.getId();
										}
										errIp.setConflictType(ConflictType.IP_BEIAN_UPLOAD_CONFLICT_ERROR.getTypeValue());
										errIp.setRegionIds(ipBean1.getStorageType() + "_" + ipBean1.getTflag() + "_" + ipBean1.getId());
										errIp.setJsonStr(ipBean1.getConflictMsg());
										errIp.setStorageType(ipBean1.getStorageType());
//										this.mergeConflictSeg(StorageType.CONFLICT.toString(), ipSeg, ip, mapKey, conflictMap);
										errIpSet.add(errIp);
										errIpBeanSet.add(ipBean1);
//									}
								}
							}else{
								continue;
							}
						}//end for
					}else if(checkType.contains("ALLOCATE")){
						for(IpBean ipBean1 : list1){
							if(null==ipBean1.getIsPass()|| ipBean1.getIsPass()== true){
								boolean flag = false;
								if(ipBean1.getFpfs().equals(IpAllocateType.RE_ALLOCATE.getTypeValue())){
									for(IpBean ipBean2 : list2){
										if(null==ipBean2.getIsPass()|| ipBean2.getIsPass()== true){
											if(!ipBean1.getFpdx().equals(ipBean2.getFpdx()) || !ipBean1.getBbdw_id().equals(ipBean2.getBbdw_id())){
												flag = false;
											}else{
												flag = true;
												if(ipBean1.getStorageType().equals(IpCheckConstants.BEIAN)){
//													String mapkey2 = (ipBean2.getBbdw_id() > ipBean2.getFpdx() ?  ipBean2.getBbdw_id() + "_" + ipBean2.getFpdx() : ipBean2.getFpdx() + ipBean2.getBbdw_id()) + "_" + ipBean2.getStorageType() + "_" + ipBean2.getTflag() + "_" + ipBean2.getId();
//													mergeSeg(StorageType.ALLOCATE_CORRECT.toString(), ipBean2, ip, mapkey2, correctMap);
													rightIpBeanSet.add(ipBean2);
//													obj.setRightIpBean(ipBean2);
												}else if(ipBean1.getStorageType().equals(IpCheckConstants.UPLOAD)){
//													String mapkey1 = (ipBean1.getBbdw_id() > ipBean1.getFpdx() ?  ipBean1.getBbdw_id() + "_" + ipBean1.getFpdx() : ipBean1.getFpdx() + ipBean1.getBbdw_id()) + "_" + ipBean1.getStorageType() + "_" + ipBean1.getTflag() + "_" + ipBean1.getId();
//													this.mergeSeg(StorageType.ALLOCATE_CORRECT.toString(), ipBean1, ip, mapkey1, correctMap);
//													obj.setRightIpBean(ipBean1);
													rightIpBeanSet.add(ipBean1);
												}
												break;
											}
										}else{
											continue;
										}
									}
									if(!flag){
										ipBean1.setIsPass(false);
//										if(redisDao.hget(IpCheckConstants.CONFLICT_DATA_KEY, ip.toString()) == null){
										ErrIp errIp = new ErrIp();
//											String mapKey = "";
//											if(ipBean1.getBbdw_id() > ipBean1.getFpdx()){
//												mapKey = ipBean1.getBbdw_id() + "_" + ipBean1.getFpdx() +  "_" + ipBean1.getStorageType() + "_" + ipBean1.getTflag() + "_" + ipBean1.getId();
												errIp.setBbdws(ipBean1.getBbdw_id().toString());
//											}else{
////												mapKey = ipBean1.getFpdx() + "_" + ipBean1.getBbdw_id() + "_" + ipBean1.getStorageType() + "_" + ipBean1.getTflag() + "_" + ipBean1.getId();
//												errIp.setBbdws(ipBean1.getBbdw_id().toString());
//											}
											errIp.setBbdws(ipBean1.getBbdw_id().toString());
											errIp.setConflictType(ConflictType.IP_BEIAN_UPLOAD_CONFLICT_ERROR.getTypeValue());
											errIp.setRegionIds(ipBean1.getStorageType() + "_" + ipBean1.getTflag() + "_" + ipBean1.getId());
											errIp.setJsonStr(ipBean1.getConflictMsg());
											errIp.setStorageType(ipBean1.getStorageType());
//											this.mergeConflictSeg(StorageType.CONFLICT.toString(), ipSeg, ip, mapKey, conflictMap);
											obj.setErrInfo(errIp);
											errIpSet.add(errIp);
											errIpBeanSet.add(ipBean1);
//										}
									}
								}else{
									IpBean temp = null;
									for(IpBean ipBean2 : list2){
										
										if(null==ipBean2.getIsPass()|| ipBean2.getIsPass()== true){
//											if(ipBean1.getBbdw_id().equals(ipBean2.getBbdw_id())){
//												ipBean2.setIsPass(false);
//											}
											if(ipBean1.getFpfs().equals(IpAllocateType.HOLDING_BY_SELF.getTypeValue())){
												flag = true;
												ipBean2.setIsPass(true);
												if(ipBean1.getStorageType().equals(IpCheckConstants.BEIAN)){
//													String mapkey2 = ipBean2.getBbdw_id() + "_" + ipBean2.getStorageType() + "_" + ipBean2.getTflag() + "_" + ipBean2.getId();
//													this.mergeSeg(StorageType.ALLOCATE_CORRECT.toString(), ipBean2, ip, mapkey2, correctMap);
//													obj.setRightIpBean(ipBean2);
													rightIpBeanSet.add(ipBean2);
												}else if(ipBean1.getStorageType().equals(IpCheckConstants.UPLOAD)){
//													String mapkey1 = ipBean1.getBbdw_id() + "_" + ipBean1.getStorageType() + "_" + ipBean1.getTflag() + "_" + ipBean1.getId();
//													this.mergeSeg(StorageType.ALLOCATE_CORRECT.toString(), ipBean1, ip, mapkey1, correctMap);
//													obj.setRightIpBean(ipBean1);
													rightIpBeanSet.add(ipBean1);
												}
												break;
											}
											else if(ipBean1.getBbdw_id().equals(ipBean2.getBbdw_id()) && ipBean1.getFpfs().equals(ipBean2.getFpfs())){
												flag = true;
												ipBean2.setIsPass(true);
												if(ipBean1.getStorageType().equals(IpCheckConstants.BEIAN)){
//													String mapkey2 = ipBean2.getBbdw_id() + "_" + ipBean2.getStorageType() + "_" + ipBean2.getTflag() + "_" + ipBean2.getId();
//													mergeSeg(StorageType.ALLOCATE_CORRECT.toString(), ipBean2, ip, mapkey2, correctMap);
//													obj.setRightIpBean(ipBean2);
													rightIpBeanSet.add(ipBean2);
												}else if(ipBean1.getStorageType().equals(IpCheckConstants.UPLOAD)){
//													String mapkey1 = ipBean1.getBbdw_id() + "_" + ipBean1.getStorageType() + "_" + ipBean1.getTflag() + "_" + ipBean1.getId();
//													this.mergeSeg(StorageType.ALLOCATE_CORRECT.toString(), ipBean1, ip, mapkey1, correctMap);
//													obj.setRightIpBean(ipBean1);
													rightIpBeanSet.add(ipBean1);
												}
												break;
											}else if(!ipBean2.getFpfs().equals(IpAllocateType.RE_ALLOCATE.getTypeValue())){
												flag = false;
												ipBean2.setIsPass(false);
												temp = ipBean2;
											}
										}
									}	
									if(!flag){
										ipBean1.setIsPass(false);
//										if(redisDao.hget(IpCheckConstants.CONFLICT_DATA_KEY, ip.toString()) == null){
											ErrIp errIp = new ErrIp();
											if(temp!=null){
												if(ipBean1.getBbdw_id().toString().equals(temp.getBbdw_id().toString())){
													errIp.setBbdws(ipBean1.getBbdw_id().toString());
												}else {
													errIp.setBbdws(ipBean1.getBbdw_id().toString()+","+temp.getBbdw_id().toString());
												};
												errIp.setConflictType(ConflictType.IP_BEIAN_UPLOAD_CONFLICT_ERROR.getTypeValue());
												errIp.setRegionIds(ipBean1.getStorageType() + "_" + ipBean1.getTflag() + "_" + ipBean1.getId()+","+temp.getStorageType() + "_" + temp.getTflag() + "_" + temp.getId());
												errIp.setJsonStr(ipBean1.getConflictMsg()+"\001"+temp.getConflictMsg());
												errIp.setStorageType(temp.getStorageType());
//												String mapKeyTemp = temp.getBbdw_id().toString() + "_" + temp.getStorageType() + "_" + temp.getTflag() + "_" + temp.getId();
//												this.mergeConflictSeg(StorageType.CONFLICT.toString(), ipSeg, ip, mapKeyTemp, conflictMap);
												errIpSet.add(errIp);
												errIpBeanSet.add(ipBean1);
											}else {
												errIp.setBbdws(ipBean1.getBbdw_id().toString());
												errIp.setConflictType(ConflictType.IP_BEIAN_UPLOAD_CONFLICT_ERROR.getTypeValue());
												errIp.setRegionIds(ipBean1.getStorageType() + "_" + ipBean1.getTflag() + "_" + ipBean1.getId());
												errIp.setJsonStr(ipBean1.getConflictMsg());
												errIp.setStorageType(ipBean1.getStorageType());
//												String mapKey = ipBean1.getBbdw_id().toString() + "_" + ipBean1.getFpdx().toString() + "_" + ipBean1.getStorageType() + "_" + ipBean1.getTflag() + "_" + ipBean1.getId();
//												mergeConflictSeg(StorageType.CONFLICT.toString(), ipSeg, ip, mapKey, conflictMap);
												errIpSet.add(errIp);
												errIpBeanSet.add(ipBean1);
											}
//										}
									}
								}
							}else{
								continue;
							}
						}
					}
				
				}
			}
		return obj;
		}
	
	public static void deal(ObjectInfo objectInfo,Set<Tuple3<Object,Boolean,Long>> resultSet,Set<IpBean> errIpBeanSet,Set<IpBean> rightTempIpBeanSet,long ip){
		for(ErrIp errIp:objectInfo.getErrIpSet()){
			resultSet.add(new Tuple3<Object,Boolean,Long>(errIp,false,ip));
		}
		for(IpBean bean:objectInfo.getErrIpBeanSet()){
			errIpBeanSet.add(bean);
		}
		for(IpBean bean:objectInfo.getRightIpBeanSet())
			rightTempIpBeanSet.add(bean);
	}
}
